<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/8/24
 * Time: 上午10:49
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ShareLog extends Model
{
    protected $table = 'share_log';
    public $timestamps = false;
    public $fillable = ['member_id','article_id','date','nums','types'];

    //关联文章
    public function g_article()
    {
        return $this->belongsTo(Article::class, 'article_id');
    }


    /**
     * 添加分享记录
     * @param $member_id  用户id
     * @param $article_id 文章id
     * @param $type 类型 0内容 1小程序
     */
    public function addShare($member_id, $article_id, $type=0)
    {
        $today = date('Y-m-d');
        $share_info = ShareLog::where('member_id',$member_id)
            ->where('article_id', $article_id)
            ->where('date', $today)
            ->where('types', $type)
            ->first();
        if(!empty($share_info) && $type == 0) {
            $share_info->nums += 1;
            $res = $share_info->save();

            return $res ? 'update' : false;
        }else {
            $insert = [
                'member_id' => $member_id,
                'article_id' => $article_id,
                'date' => $today,
                'nums' => 1,
                'types' => $type
            ];
            $res = ShareLog::create($insert);
        }

        return $res ? 'insert' : false;
    }

}